package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.dto.DictPageDto;
import com.hoongs.v1.entity.Dict;
import com.hoongs.v1.service.DictService;
import com.hoongs.v1.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dict")
@Validated
public class DictController {

    @Resource
    private DictService dictService;

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated Dict dict, BindingResult bindingResult) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : dictService.add(dict);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return dictService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(Dict dict) {
        return dictService.update(dict);
    }


    @GetMapping("/selectById")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectById(String id) {
        return dictService.selectById(id);
    }

    @GetMapping("/selectList")
    public Result selectList(String name, String parentId,  String parentName) {
        return dictService.selectList(name, parentId, parentName);
    }

    @GetMapping("/selectPageList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectPageList(DictPageDto dictDto) {
        return dictService.selectPageList(dictDto);
    }

}
